Using Flash in Visual Basic


This article was contributed by Sameers (theAngrycodeR).

Category: Control Demonstration

Versions: VB5, VB6

No doubt, that the Flash is a very powerfull tool for the web page designing. But what if you are able to use the flash interface in Visual Basic Desktop applications? Sure, it will enhance the look of you application. The interface, which is very difficult to design in VB itself. Here is an example of building application using both Flash and Visual Basic

If you look into the Components, you will see a control named Shockwave Flash which can be used to embed your Flash Movie into Visual Basic application. Just Include that control and place an instance on your form. This control has a property named "Movie", Set your Shockwave Files (.swf) path for this property and just run your application. You will see that the Flash Movie is loaded on your form. That's So simple.

Now the Question is that how can you trap the events raised by Flash in VB like Click on a Button or to Set or Get property of any instance, Play or Stop any Movie etc etc. So, this simple example demonstrate it all. If you see the Source code, then you will get that the heart of the code is an event of Shockwave Flash Control named FSCommand. Let's discuss how to use this FSCommand and how to generate this FSCommand in Flash.

When you are designing a Flash Movie, then what you have to do is to write a little script for the objects whom events you want to handle. Like, if you want to know when a Flash Button is clicked, you have to write a little script (Action) for that button. Just right click on the Button (Or any Object) and Select Action. In the window popped up, write his code

on (release) {
   fscommand ("ButtonClick", "cmdNext");
   }

FSCommand which can be selected from the Basic Actions recieves two parameters (generally although it's up to you how much parameters you provides). FSCommand is actually the script which allows Flash Movie to communicate with the Host player. So, in case of above example, you passed two parameters to FSCommand, ButtonClick and cmdNext. ButtonClick will tell the host player that some button is clicked and cmdNext that the name of the Button is cmdNext. Don't take it pet syntax that whenever a buton is clicked, you must have to follow these parameter sequece. You can use your own which you mean better. It will more clear when you will handle them.

Now come to our VB Programme. Our Showckwave Flash Control has an event named FSCommand. Whenever the script which I mentioned above is executed, FSCommand Event will be raised. You can code yourself to handle it. Like see my code.

Private Sub SWFlash_FSCommand(ByVal command As String, ByVal args As String)
   
   If command = "ButtonClick" Then
	   Select Case args   
		   Case "cmdNext": SWFlash.Next 'For Moving to Next Scene
		   ......
		   ...... [more code goes here]
   

As you can see above, FSCommand is recieving two parameters, a command and args. then I selected command to see that either it was ButtonClick, If so then I checked the args, if that is cmdNext, if that is then I told my control to move Next. (which is to move to next scene). That's so simple. I think it will be clear now that how can you handle events of Flash in Visual Basic. And you must have got now that it is not necessary to Pass ButtonClick and cmdNext parameters. You can use anything which can be easily recognized by you.

The rest of the application is fairly simple and I also comment that on each step. So it will be more simple to understand that. I hope you will like it.

Downloads

Download source - 65.4 Kb

History

Date Posted: July 11, 2002